<?php
/**
 * Controller_Base 是后台控制器的基类，提供一些公共方法
 *
 * @package Movoin Blog
 * @subpackage Admin
 *
 */
class Controller_Base extends FLEA_Controller_Action {

    /*
     * 模板引擎实例
     */
    var $_tpl = null;

    /**
     * 构造函数
     *
     * @return Controller_Base
     */
    function Controller_Base() {
    	/*
    	 * Smarty模板句柄
    	 */
        $this->_tpl = & $this->_getView();
        /*
         * 缓存博客配置信息
         */
        $cacheid = 'settings.admin';
        $settings = FLEA::getCache($cacheid, 9000, true, true);
        if (!is_array($settings)) {
        	$modelSettings =& FLEA::getSingleton('Model_Settings');
            $settings = $modelSettings->getSettings();
            FLEA::writeCache($cacheid, $settings, true);
            unset($modelSettings);
        }
        FLEA::setAppInf('settings', $settings);
        $this->_tpl->assign('settings', $settings);
        unset($settings);
    }

    /**
     * 根据数据表的元数据返回一个数组，这个数组包含所有需要初始化的数据，可以用于界面显示
     *
     * @param array $meta
     * @return array
     */
    function _prepareData(& $meta) {
        $data = array ();
        foreach ( $meta as $m ) {
            if (isset ( $_POST [$m ['name']] )) {
                $data [$m ['name']] = $_POST [$m ['name']];
            } else {
                if (isset ( $m ['defaultValue'] )) {
                    $data [$m ['name']] = $m ['defaultValue'];
                } else {
                    $data [$m ['name']] = null;
                }
            }
        }
        return $data;
    }

    /**
     * 返回用 _setBack() 设置的 URL
     */
    function _goBack() {
        $url = $this->_getBack ();
        unset ( $_SESSION ['BACKURL'] );
        redirect ( $url );
    }

    /**
     * 设置返回点 URL，稍后可以用 _goBack() 返回
     */
    function _setBack() {
        $_SESSION ['BACKURL'] = $_SERVER ['QUERY_STRING'];
    }

    /**
     * 获取返回点 URL
     *
     * @return string
     */
    function _getBack() {
        if (isset ( $_SESSION ['BACKURL'] )) {
            $url = $this->rawurl ( $_SESSION ['BACKURL'] );
        } else {
            $url = $this->_url ();
        }
        return $url;
    }

    /**
     * 直接提供查询字符串，生成 URL 地址
     *
     * @param string $queryString
     * @return string
     */
    function rawurl($queryString) {
        if (substr ( $queryString, 0, 1 ) == '?') {
            $queryString = substr ( $queryString, 1 );
        }
        return $_SERVER ['SCRIPT_NAME'] . '?' . $queryString;
    }

}